package main

import (
	"GoTestHost/Services"
	"GoTestHost/tree"
	"GoTestHost/util"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"

	httptransport "github.com/go-kit/kit/transport/http"
	mymux "github.com/gorilla/mux"
	"go.uber.org/zap"
)

func main() {
	fmt.Println("hello")
	var root tree.Node
	root = tree.Node{Value: 3}
	root.Left = &tree.Node{}
	root.Right = &tree.Node{5, nil, nil}
	root.Right.Left = new(tree.Node)
	fmt.Println(root.Value)
	logger, _ := zap.NewProduction()
	logger.Warn("warning test")
	user := Services.UserService{}
	endp := Services.GenUserEndpoint(user)

	serverHander := httptransport.NewServer(endp, Services.DecodeUserRequest, Services.EncodeUserResponse)
	r := mymux.NewRouter()
	//r.Handle(`/user/{uid:\d+}`, serverHander)
	r.Methods("GET").Path(`/user/{uid:\d+}`).Handler(serverHander)
	r.Path("/health").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		w.Header().Set("Content-type", "application/json")
		w.Write([]byte(`{"status":"ok"}`))
	})

	errChan := make(chan error)
	c := util.ConsulAPI{}
	go (func() {
		c.RegServer()
		err := http.ListenAndServe("127.0.0.1:8090", r)
		if err != nil {
			errChan <- err
		}
	})()

	go (func() {
		sig_c := make(chan os.Signal)
		signal.Notify(sig_c, syscall.SIGINT, syscall.SIGTERM)
		errChan <- fmt.Errorf("%s", <-sig_c)
	})()
	getErr := <-errChan
	c.Unregservice()
	log.Println(getErr)

}
